import { AbsSvgUnitParser } from "../absunitparser"
import rectangle from "../../../../../canvas/common/struct/basestruct/rectangle";
import globalconst from "../../../../../canvas/common/globalConst";
var global = globalconst.GlobalConst;

class RectParser extends AbsSvgUnitParser {
    constructor() {
        super();
        this.type = global.BaseUnitRectangle << 16;
    }
    parse(mainEle) {
        let unitInfo = new rectangle.Rectangle();
        unitInfo.type = this.type;

        let x = 0;
        let y = 0;
        let width = 0;
        let height = 0;
        const attributes = mainEle.attributes;
        for (let i = 0; i < attributes.length; i++) {
            const attr = attributes[i];
            if (attr.name == 'x') {
                x = parseFloat(attr.value);
            }
            else if (attr.name == 'y') {
                y = parseFloat(attr.value);
            }
            else if (attr.name == 'width') {
                width = parseFloat(attr.value);
            }
            else if (attr.name == 'height') {
                height = parseFloat(attr.value);
            }
        }
        this.parseCommonProp(unitInfo, mainEle);
        let pt1 = { x, y };
        this.chg2SysCoord(pt1);
        unitInfo.pos.addPoint(pt1);

        let pt2 = { x, y: y + height };
        this.chg2SysCoord(pt2);
        unitInfo.pos.addPoint(pt2);

        let pt3 = { x: x + width, y: y + height };
        this.chg2SysCoord(pt3);
        unitInfo.pos.addPoint(pt3);

        let pt4 = { x: x + width, y };
        this.chg2SysCoord(pt4);
        unitInfo.pos.addPoint(pt4);

        unitInfo.strPosition = unitInfo.pos.toString();
        return unitInfo;
    }
}

export { RectParser }